package com.example.bittraffic;

import android.os.Bundle;
import android.os.Vibrator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Activity_Bus_PlanB extends Activity {
	WebView web;
	Context context = Activity_Bus_PlanB.this;
	String url = "http://bus.gjcity.net:7001/app/mobile/m_main.jsp";
	ProgressBar progressBar;
	final Activity activity = this;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bus_planb);

		web = (WebView) findViewById(R.id.webview_bus_planb);
		progressBar = (ProgressBar) this.findViewById(R.id.bar);
		WebSettings set = web.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);

		web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

		web.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			};

			@Override
			public void onPageStarted(WebView view, String url,
					android.graphics.Bitmap favicon) {
				super.onPageStarted(view, url, favicon); // 페이지 로딩 시작
				progressBar.setVisibility(View.VISIBLE);
			};

			@Override
			public void onPageFinished(WebView view, String url) { // 페이지 로딩 완료
				super.onPageFinished(view, url);
				progressBar.setVisibility(View.INVISIBLE);
			};

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String fallingUrl) {
				Toast.makeText(activity, "오류: " + description,
						Toast.LENGTH_LONG).show();
			}
		});

		web.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int Progress) {
				activity.setProgress(Progress * 100);
			}
		});
		web.loadUrl(url);
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// getMenuInflater().inflate(R.menu.activity__bus__plan_b, menu);
	// return true;
	// }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.setQwertyMode(true);

		// SubMenu sm = menu.addSubMenu(0, 0, 0, "버스");
		// sm.add(1, 0, 0, "버스0");
		// sm.add(1, 1, 1, "버스1");

		// MenuItem mi = menu.add(0, 0, 0, "버스");
		// mi.setAlphabeticShortcut('a');

		menu.add(0, 2, 2, "유스퀘어").setAlphabeticShortcut('a')
				.setIcon(R.drawable.image_bus);

		menu.add(0, 0, 0, "지하철").setAlphabeticShortcut('b')
				.setIcon(R.drawable.image_subway);

		menu.add(0, 1, 1, "택시").setAlphabeticShortcut('c')
				.setIcon(R.drawable.image_taxi);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 2) {
			// Intent intent = new Intent(Intent.ACTION_VIEW);
			// intent.setData(Uri.parse("http://m.usquare.co.kr"));
			// startActivity(intent);
			// finish();
			Intent intent = new Intent(getApplicationContext(),
					Activity_Bus_Usquare.class);
			startActivity(intent);
			finish();

		}
		if (item.getItemId() == 0) {
			Intent intent = new Intent(getApplicationContext(),
					Activity_Subway.class);
			// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		if (item.getItemId() == 1) {
			Intent intent = new Intent(getApplicationContext(),
					Activity_Taxi.class);
			// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		return false;
	}

}